package com.module.codeBind.dao;

import java.util.List;

import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.JsonMsg;
import base.util.Tools;

import com.module.codeBind.vo.BatchCodeItem;

public class BatchCodeItemDao  extends GenericHibernateDao<BatchCodeItem,String>{
	private static BatchCodeItemDao dao = new BatchCodeItemDao();
	private BatchCodeItemDao(){}
	public static BatchCodeItemDao getInstance(){
		return dao;
	}
	public boolean save(Session session,List<BatchCodeItem> itemList,String batchCodeId){
		this.deleteByQuery(session,"delete from BatchCodeItem as obj where obj.batchCodeId='"+batchCodeId+"'");
		for(BatchCodeItem item:itemList){
			item.setBatchCodeId(batchCodeId);
			session.save(item);
		}
		return true;
	}
	public JsonMsg verify(Session session,List<BatchCodeItem> itemList,String batchCodeId){
		JsonMsg jsonMsg = new JsonMsg(true);
		int len = itemList.size();
		for(int i=0;i<len;i++){
			BatchCodeItem item = itemList.get(i);
			for(int j=i+1;j<len;j++){
				BatchCodeItem bci = itemList.get(j);
				if(item.getVolumeNo().equals(bci.getVolumeNo())){
					if(item.getStartSeq()<=bci.getStartSeq() && item.getEndSeq()>=bci.getStartSeq()){
						jsonMsg.setFailMsg("卷【"+bci.getVolumeNo()+"】,开始码【"+bci.getStartVolumeCode()+"】存在重复，请检查!");
						return jsonMsg;
					}else if(item.getStartSeq()<=bci.getEndSeq() && item.getEndSeq()>=bci.getEndSeq()){
						jsonMsg.setFailMsg("卷【"+bci.getVolumeNo()+"】,开始码【"+bci.getStartVolumeCode()+"】,结束码【"+bci.getEndVolumeCode()+"】之间存在嵌套，请检查");
						return jsonMsg;
					}else if(item.getStartSeq()>=bci.getStartSeq() && item.getEndSeq()<=bci.getEndSeq()){
						jsonMsg.setFailMsg("卷【"+bci.getVolumeNo()+"】,开始码【"+bci.getStartVolumeCode()+"】,结束码【"+bci.getEndVolumeCode()+"】之间存在嵌套，请检查");
						return jsonMsg;
					}else if(item.getStartSeq()<=bci.getEndSeq() && item.getEndSeq()>=bci.getEndSeq()){
						jsonMsg.setFailMsg("卷【"+bci.getVolumeNo()+"】,结束码【"+bci.getStartVolumeCode()+"】存在重复，请检查!");
						return jsonMsg;
					}
				}
				
			}
		}
		for(BatchCodeItem item:itemList){
			String hql = "select bci from BatchCode as bc,BatchCodeItem as bci where bc.id= bci.batchCodeId and bc.status!='D' and bci.volumeNo='"+item.getVolumeNo()+"' ";
			if(Tools.isValid(batchCodeId)){
				hql+=" and bci.batchCodeId !='"+batchCodeId+"'";
			}
			List<BatchCodeItem> list = this.findBySQL(session, hql);
			for(BatchCodeItem bci:list){
				if(item.getVolumeNo().equals(bci.getVolumeNo())){
					if(item.getStartSeq()<=bci.getStartSeq() && item.getEndSeq()>=bci.getStartSeq()){
						jsonMsg.setFailMsg("卷【"+item.getVolumeNo()+"】,开始码【"+item.getStartVolumeCode()+"】存在重复，请检查");
						return jsonMsg;
					}else if(item.getStartSeq()<=bci.getEndSeq() && item.getEndSeq()>=bci.getEndSeq()){
						jsonMsg.setFailMsg("卷【"+item.getVolumeNo()+"】,开始码【"+item.getStartVolumeCode()+"】,结束码【"+item.getEndVolumeCode()+"】之间存在嵌套，请检查");
						return jsonMsg;
					}else if(item.getStartSeq()>=bci.getStartSeq() && item.getEndSeq()<=bci.getEndSeq()){
						jsonMsg.setFailMsg("卷【"+bci.getVolumeNo()+"】,开始码【"+bci.getStartVolumeCode()+"】,结束码【"+bci.getEndVolumeCode()+"】之间存在嵌套，请检查");
						return jsonMsg;
					}else if(item.getStartSeq()<=bci.getEndSeq() && item.getEndSeq()>=bci.getEndSeq()){
						jsonMsg.setFailMsg("卷【"+bci.getVolumeNo()+"】,结束码【"+bci.getStartVolumeCode()+"】存在重复，请检查!");
						return jsonMsg;
					}
				}
			}
		}
		return jsonMsg;
	}
	
}
